package com.tjcd.project.library_base.room;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.tjcd.project.library_base.room.bean.UserBean;
import com.tjcd.project.library_base.room.converter.DateConverter;
import com.tjcd.project.library_base.room.dao.UserDao;


/**
 * @Author jiangKunKun
 * @CreateDate 2022/2/21
 * @Description
 * @SpittingGrooves
 */
@Database(entities =  {UserBean.class}, version = 1, exportSchema = false,
        autoMigrations = {
//                @AutoMigration(from = 1, to = 2),
//                @AutoMigration(from = 2, to = 3),
//                @AutoMigration(from = 3, to = 4),
        })
@TypeConverters(DateConverter.class)
public abstract class CustomRoomDatabase extends androidx.room.RoomDatabase {
    public static final String DATABASE_NAME = "数据库名字.db";
    private static CustomRoomDatabase sInstance;

    /**
     * 用户数据库 Dao 层
     * @return
     */
    public abstract UserDao mUserDao();

    public static CustomRoomDatabase getInstance(Context context) {
        if (sInstance == null) {
            synchronized (CustomRoomDatabase.class) {
                if (sInstance == null) {
                    sInstance = buildDatabase(context);
                }
            }
        }
        return sInstance;
    }

    private static CustomRoomDatabase buildDatabase(final Context appContext) {
        return Room.databaseBuilder(appContext, CustomRoomDatabase.class, DATABASE_NAME)
                .allowMainThreadQueries()
                .addMigrations()
                // 迁移数据库如果发生错误，将会重新创建数据库，而不是发生崩溃
                .fallbackToDestructiveMigration() // 自动删除并重建数据库
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                    }

                    @Override
                    public void onOpen(@NonNull SupportSQLiteDatabase db) {
                        super.onOpen(db);
                    }

                })
                .build();
    }

    private static Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database){
            //执行升级相关操作
            /**
             *             database.execSQL("CREATE TABLE temp_Student (" +
             *                     "id INTEGER PRIMARY KEY NOT NULL," +
             *                     "name TEXT," +
             *                     "age TEXT)");
             *             database.execSQL("INSERT INTO temp_Student (id, name, age) " +
             *                     "SELECT id, name, age FROM Student");
             *             database.execSQL("DROP TABLE Student");
             *             database.execSQL("ALTER TABLE temp_Student RENAME TO Student");
             */
        }
    };
}
